package com.kmxd.ams.controller.arrange;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kmxd.ams.client.arrange.IProjectSvc;
import com.kmxd.ams.core.common.enums.LogLevelEnum;
import com.kmxd.ams.core.common.enums.LogTypeEnum;
import com.kmxd.ams.core.log.annotation.AutoLog;
import com.kmxd.ams.core.result.R;
import com.kmxd.ams.core.util.SecurityUtil;
import com.kmxd.ams.infra.arrange.entity.Project;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 项目级 前端控制器
 *
 * @author ZuoLG
 * @since 2022-07-20
 */
@RestController
@Tag(name = "项目级")
@RequestMapping("/arrange/project")
@AllArgsConstructor
public class ProjectCtl {

  private IProjectSvc projectService;

  /** 分页查询 */
  @Operation(summary = "分页查询")
  @RequestMapping("/selectPage")
  @AutoLog(module = "项目级", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "分页查询")
  public R<Page<Project>> selectPage(Page reqPage, Project req) {
    return R.success(projectService.selectPage(reqPage, req), req);
  }

  /** 列表查询 */
  @Operation(summary = "列表查询")
  @RequestMapping("/selectList")
  @AutoLog(module = "项目级", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "列表查询")
  public R<List<Project>> selectList(Project req) {
    return R.success(projectService.selectList(req));
  }

  /** 列表查询 */
  @Operation(summary = "列表全部查询")
  @GetMapping("/selectAllList")
  @AutoLog(module = "项目级", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "列表全部查询")
  public R<List<Project>> selectAllList(Project req) {
    return R.success(projectService.selectAllList(req));
  }

  /** 新增 */
  @Operation(summary = "新增")
  @PostMapping("/add")
  @SaCheckPermission("arrange:project:add")
  @AutoLog(module = "项目级", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "新增")
  public R<Void> add(@Validated @RequestBody Project req) {
    req.setFondsId(
        ObjectUtil.isEmpty(req.getFondsId()) ? SecurityUtil.getFondsId() : req.getFondsId());
    projectService.add(req);
    return R.success();
  }

  /** 修改 */
  @Operation(summary = "修改")
  @PostMapping("/edit")
  @SaCheckPermission("arrange:project:edit")
  @AutoLog(module = "项目级", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.HIGH, title = "修改")
  public R<Void> edit(@Validated @RequestBody Project req) {
    projectService.edit(req);
    return R.success();
  }

  /** 删除 */
  @Operation(summary = "删除")
  @PostMapping("/remove")
  @SaCheckPermission("arrange:project:remove")
  @AutoLog(module = "项目级", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.HIGH, title = "删除")
  public R<Void> remove(@Validated @RequestBody Project req) {
    projectService.remove(req);
    return R.success();
  }

  /** 查看 */
  @Operation(summary = "查看")
  @PostMapping("/view")
  @AutoLog(module = "项目级", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "查看")
  public R<Project> view(@Validated @RequestBody Project req) {
    return R.success(projectService.view(req));
  }

  /** 导出 */
  @Operation(summary = "导出")
  @GetMapping("/export")
  @AutoLog(module = "项目级", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.HIGH, title = "导出")
  @SneakyThrows
  public void export(Project req, HttpServletResponse response) {
    String filename = "项目级列表" + "_" + System.currentTimeMillis() + ".xlsx";
    response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("UTF-8");
    response.setHeader(
        "Content-Disposition",
        "attachment;filename=" + URLEncoder.encode(filename, StandardCharsets.UTF_8.name()));
    EasyExcel.write(response.getOutputStream())
        .head(Project.class)
        .sheet("项目级列表")
        .doWrite(projectService.selectList(req));
  }
}
